package com.ddm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddm.dto.page.TermPageQueryDTO;
import com.ddm.entity.Standard;
import com.ddm.entity.Term;
import com.ddm.mapper.TermMapper;
import com.ddm.result.PageResult;
import com.ddm.service.TermService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 术语表 服务实现类
 * </p>
 *
 * @author 丁丁猫
 * @since 2025-01-13
 */
@Service
public class TermServiceImpl extends ServiceImpl<TermMapper, Term> implements TermService {

    @Autowired
    private TermMapper termMapper;
    /**
     * 根据关键词分页查询术语
     * @param termPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(TermPageQueryDTO termPageQueryDTO) {
        IPage<Term> page = new Page(termPageQueryDTO.getPage(), termPageQueryDTO.getPageSize());

        LambdaQueryWrapper<Term> lqw = new LambdaQueryWrapper();
        //按照id升序排列
        lqw.eq(Term::getStandardId, termPageQueryDTO.getStandardId())
                .like(StringUtils.isNotEmpty(termPageQueryDTO.getKey()), Term::getCName, termPageQueryDTO.getKey())
                .orderByAsc(Term::getId);
        termMapper.selectPage(page, lqw);
        long total = page.getTotal();
        List<Term> records = page.getRecords();
        //设置数据
        PageResult pageResult = new PageResult(total, records);
        return pageResult;
    }

    /**
     * 根据标准id查询术语
     * @param id
     * @return
     */
    @Override
    public List<Term> getByStandardId(Long id) {
        LambdaQueryWrapper<Term> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Term::getStandardId, id);
        return termMapper.selectList(lqw);
    }
}
